package com.ruoyi.amm.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Excel;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.amm.domain.Amm;
import com.ruoyi.amm.service.IAmmService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
/**
 * 仪器管理Controller
 * 
 * @author pyt
 * @date 2022-12-14
 */
@RestController
@RequestMapping("/amm/amm")
public class AmmController extends BaseController
{
    @Autowired
    private IAmmService ammService;

    /**
     * 查询仪器管理列表
     */
    @PreAuthorize("@ss.hasPermi('amm:amm:list')")
    @GetMapping("/list")
    public TableDataInfo list(Amm amm)
    {
        startPage();
        List<Amm> list = ammService.selectAmmList(amm);
        return getDataTable(list);
    }

    /**
     * 导出仪器管理列表
     */
    @PreAuthorize("@ss.hasPermi('amm:amm:export')")
    @Log(title = "导出仪器管理列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        List<Amm> list = ammService.selectAmmList(new Amm());
        ExcelUtil<Amm> util = new ExcelUtil<Amm>(Amm.class);
        util.exportExcel(response, list, "仪器管理数据");

    }

    /**
     * 获取仪器管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('amm:amm:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ammService.selectAmmById(id));
    }

    /**
     * 新增仪器管理
     */
    @PreAuthorize("@ss.hasPermi('amm:amm:add')")
    @Log(title = "仪器管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Amm amm)
    {
        return toAjax(ammService.insertAmm(amm));
    }

    /**
     * 修改仪器管理
     */
    @PreAuthorize("@ss.hasPermi('amm:amm:edit')")
    @Log(title = "仪器管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Amm amm)
    {
        return toAjax(ammService.updateAmm(amm));
    }

    /**
     * 删除仪器管理
     */
    @PreAuthorize("@ss.hasPermi('amm:amm:remove')")
    @Log(title = "仪器管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ammService.deleteAmmByIds(ids));
    }



    @Log(title = "仪器管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('amm:amm:import')")
    @PostMapping("/import")
    @Transactional
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Amm> util = new ExcelUtil<Amm>(Amm.class);
        List<Amm> ammList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        for (Amm amm : ammList) {
            ammService.insertAmm(amm);
        }
        return success("导入完成!");
    }


    @GetMapping("/getImportTemplate")
    public void getImportTemplate(HttpServletResponse response)
    {
        ExcelUtil<Amm> util = new ExcelUtil<Amm>(Amm.class);
        util.exportExcel(response,new ArrayList<>() ,"仪器仪表-导入模板");
    }
}
